﻿using System;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class PlanVirtual : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("NA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.Form["KEY"] == null)
                {
                    lblError.Text = "Invalid page call!";
                    return;
                }
                string key;
                string[] strArray;
                key = Request.Form["KEY"];
                strArray = key.Split('|');

                lblAirline.Text = strArray[0];
                lblFltNo.Text = strArray[1];
                lblID.Text = strArray[2];

                KEY.Value = lblAirline.Text + "|" + lblFltNo.Text + "|" + lblID.Text;

            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            lblError.Text = "";
            GridView1.DataBind();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            int ret_code;
            string key;
            string[] strArray;
            key = Request.Form["KEY2"];
            strArray = key.Split('|');
            try
            {
                SqlCommand cmd = new SqlCommand("bll_plan_delete_virtual_flt");
                SqlParameter para = cmd.Parameters.AddWithValue("@op_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@op_flt_no", strArray[1]);
                para = cmd.Parameters.AddWithValue("@plan_id", strArray[2]);
                para = cmd.Parameters.AddWithValue("@market_airline", strArray[3]);
                para = cmd.Parameters.AddWithValue("@market_flt_no", strArray[4]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Can not delete virtual flight in current status!";
                else if (ret_code == 2)
                    lblError.Text = "Delete failed!";
                else
                    lblError.Text = "Error " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("NA2") < 0)
            {
                lblError.Text = "unauthorized";
                return;
            }
            if (Session["AIRLINE"].ToString().Trim() == lstAirlines.SelectedValue.Trim())
            {
                lblError.Text = "Invalid market airline!";
                return;
            }
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_plan_add_virtual_flt");
                SqlParameter para = cmd.Parameters.AddWithValue("@op_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@op_flt_no", lblFltNo.Text);
                para = cmd.Parameters.AddWithValue("@plan_id", lblID.Text);
                para = cmd.Parameters.AddWithValue("@market_airline", lstAirlines.SelectedValue);
                para = cmd.Parameters.AddWithValue("@market_flt_no", txtFltNo.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Can not add virtual flight in current status!";
                else if (ret_code == 2)
                    lblError.Text = "Insert failed!";
                else if (ret_code == 3)
                    lblError.Text = "Invalid market airline!";
                else
                    lblError.Text = "Error " + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}